<?php
namespace app\api\controller;

use think\Db;
header("ACCESS-CONTROL-ALLOW-ORIGIN:http://114.215.68.205");

class Userdividend
{
/**
 * CM(三胖)
 * 每月发放彩金
 * @return Ambigous <\think\response\Json, \think\Response, \think\response\View, \think\response\Xml, \think\response\Redirect, \think\response\Jsonp, unknown, \think\Response>
 */
    public function cjDayGrant(){
        if(date('d',time()) == 1){
            Db::startTrans();
            try {
				/**todo remove zdtzxd*/
                $us_class = Db::name('us_class')->column('id,mearmoney,zxtzxd');
                $user = Db::name('username')
                					->alias('a')
                					->join('__TOUZHU_LIST_OK__ b','b.username=a.username')
                					->field('a.username,a.us_class,SUM(b.tzje) as tzje')
                					->group('a.username,a.us_class')
                					->where('tz_time','between',timeSelection(3))
                					->select();
                foreach ($user as $u){
					/**todo remove  && $us_class[$u['us_class']]['zdtzxd']>=$u['tzje']*/
                	if( $us_class[$u['us_class']]['mearmoney']>0 && $us_class[$u['us_class']]['zxtzxd']<=$u['tzje'] ){
        	           $save = Db::name('username')->where('username', $u['username'])->setInc('je', $us_class[$u['us_class']]['mearmoney']);
        	           $find_je = Db::name('username')->where('username',$u['username'])->field('je')->find();
        	           $data = array();
        	           $data['username'] = $u['username'];
        	           $data['user_class'] = $u['us_class'];
        	           $data['cjje'] = $us_class[$u['us_class']]['mearmoney'];
        	           $data['fstime'] = time();
        	           $data['beizhu'] = '系统赠送,每月1号定时彩金派送';
        	           $data['czadmin'] = '系统赠送';
        	           $data['cjhje'] = $find_je['je'];
        	          
        	           	Db::name('caijin_list')->insert($data);
        	        }
        	           Db::commit();
                }
            } catch (\Exception $e) {
                 
                Db::rollback();
                return json(array('status'=>0,'msg'=>'更新失败'));
            }
            return json(array('status'=>1,'msg'=>'更新成功'));
         }else{
			return json(array('status'=>0,'msg'=>'参数不对'));
		} 
    }
}